{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "026a3355-d301-42eb-a854-d47a4ae9e984",
   "metadata": {},
   "source": [
    "## 一、网络结构与参数定义\n",
    "- **网络层级**：输入层（2节点，无偏置）→隐藏层（2节点）→输出层（2节点）  \n",
    "- **偏置定义**：  \n",
    "  - 隐藏层偏置：$b_1$（隐藏节点1）、$b_2$（隐藏节点2）  \n",
    "  - 输出层偏置：$b_3$（输出节点1）、$b_4$（输出节点2）  \n",
    "- **已知参数**：  \n",
    "  输入：$x_1=0.5$，$x_2=1.0$；真实标签：$\\hat{y}_1=0.8$，$\\hat{y}_2=1$  \n",
    "  权重：$w_1=1$（$x_1→h_1$），$w_2=0.5$（$x_2→h_1$），$w_3=0.5$（$x_1→h_2$），$w_4=0.8$（$x_2→h_2$），$w_5=0.8$（$h_1→y_1$），$w_6=1$（$h_2→y_1$），$w_7=1.2$（$h_1→y_2$），$w_8=0.9$（$h_2→y_2$）  \n",
    "  偏置：$b_1=1.2$，$b_2=1.5$，$b_3=1$，$b_4=1.5$  \n",
    "  学习率：$\\eta=1$  \n",
    "\n",
    "\n",
    "## 二、前向传播计算\n",
    "### 1. 输入层输出（无偏置）  \n",
    "输入层直接输出原始输入：  \n",
    "$x_1' = x_1 = 0.5$  \n",
    "$x_2' = x_2 = 1.0$  \n",
    "\n",
    "\n",
    "### 2. 隐藏层计算  \n",
    "- **隐藏层输入（加权和 + 隐藏层偏置）**：  \n",
    "  $a_1^h = w_1 \\cdot x_1' + w_2 \\cdot x_2' + b_1 = 1×0.5 + 0.5×1.0 + 1.2 = 0.5 + 0.5 + 1.2 = 2.2$  \n",
    "  $a_2^h = w_3 \\cdot x_1' + w_4 \\cdot x_2' + b_2 = 0.5×0.5 + 0.8×1.0 + 1.5 = 0.25 + 0.8 + 1.5 = 2.55$  \n",
    "\n",
    "- **隐藏层输出（sigmoid激活）**：  \n",
    "  $\\sigma(t) = \\frac{1}{1+e^{-t}}$，故：  \n",
    "  $h_1 = \\sigma(a_1^h) = \\frac{1}{1+e^{-2.2}} \\approx 0.8909$  \n",
    "  $h_2 = \\sigma(a_2^h) = \\frac{1}{1+e^{-2.55}} \\approx 0.9278$  \n",
    "\n",
    "\n",
    "### 3. 输出层计算  \n",
    "- **输出层输入（加权和 + 输出层偏置）**：  \n",
    "  $a_1^o = w_5 \\cdot h_1 + w_6 \\cdot h_2 + b_3 = 0.8×0.8909 + 1×0.9278 + 1 = 0.7127 + 0.9278 + 1 = 2.6405$  \n",
    "  $a_2^o = w_7 \\cdot h_1 + w_8 \\cdot h_2 + b_4 = 1.2×0.8909 + 0.9×0.9278 + 1.5 = 1.0691 + 0.8350 + 1.5 = 3.4041$  \n",
    "\n",
    "- **输出层输出（sigmoid激活）**：  \n",
    "  $y_1 = \\sigma(a_1^o) = \\frac{1}{1+e^{-2.6405}} \\approx 0.9332$  \n",
    "  $y_2 = \\sigma(a_2^o) = \\frac{1}{1+e^{-3.4041}} \\approx 0.9677$  \n",
    "\n",
    "\n",
    "### 4. 损失函数（MSE）  \n",
    "$L = \\frac{1}{2}[(y_1 - \\hat{y}_1)^2 + (y_2 - \\hat{y}_2)^2] \\approx \\frac{1}{2}[(0.9332-0.8)^2 + (0.9677-1)^2] \\approx 0.0094$  \n",
    "\n",
    "\n",
    "## 三、反向传播计算（梯度与参数更新）\n",
    "### 1. 输出层误差项（$\\delta^o$）  \n",
    "误差项定义：$\\delta_i^o = \\frac{\\partial L}{\\partial a_i^o} = \\frac{\\partial L}{\\partial y_i} \\cdot \\frac{\\partial y_i}{\\partial a_i^o}$  \n",
    "\n",
    "- 步骤1：$\\frac{\\partial L}{\\partial y_i}$  \n",
    "  $\\frac{\\partial L}{\\partial y_1} = y_1 - \\hat{y}_1 \\approx 0.9332 - 0.8 = 0.1332$  \n",
    "  $\\frac{\\partial L}{\\partial y_2} = y_2 - \\hat{y}_2 \\approx 0.9677 - 1 = -0.0323$  \n",
    "\n",
    "- 步骤2：$\\frac{\\partial y_i}{\\partial a_i^o}$（sigmoid导数：$\\sigma'(t) = \\sigma(t)(1-\\sigma(t))$）  \n",
    "  $\\frac{\\partial y_1}{\\partial a_1^o} = y_1(1-y_1) \\approx 0.9332×(1-0.9332) \\approx 0.0623$  \n",
    "  $\\frac{\\partial y_2}{\\partial a_2^o} = y_2(1-y_2) \\approx 0.9677×(1-0.9677) \\approx 0.0313$  \n",
    "\n",
    "- 输出层误差项：  \n",
    "  $\\delta_1^o = 0.1332×0.0623 \\approx 0.0083$  \n",
    "  $\\delta_2^o = -0.0323×0.0313 \\approx -0.0010$  \n",
    "\n",
    "\n",
    "### 2. 隐藏层误差项（$\\delta^h$）  \n",
    "误差项定义：$\\delta_j^h = \\left( \\sum_{i=1}^2 \\delta_i^o \\cdot \\frac{\\partial a_i^o}{\\partial h_j} \\right) \\cdot \\frac{\\partial h_j}{\\partial a_j^h}$  \n",
    "\n",
    "- 步骤1：$\\frac{\\partial a_i^o}{\\partial h_j}$（即隐藏→输出层权重）  \n",
    "  $\\frac{\\partial a_1^o}{\\partial h_1} = w_5 = 0.8$；$\\frac{\\partial a_1^o}{\\partial h_2} = w_6 = 1$  \n",
    "  $\\frac{\\partial a_2^o}{\\partial h_1} = w_7 = 1.2$；$\\frac{\\partial a_2^o}{\\partial h_2} = w_8 = 0.9$  \n",
    "\n",
    "- 步骤2：$\\frac{\\partial h_j}{\\partial a_j^h}$（sigmoid导数）  \n",
    "  $\\frac{\\partial h_1}{\\partial a_1^h} = h_1(1-h_1) \\approx 0.8909×(1-0.8909) \\approx 0.0972$  \n",
    "  $\\frac{\\partial h_2}{\\partial a_2^h} = h_2(1-h_2) \\approx 0.9278×(1-0.9278) \\approx 0.0670$  \n",
    "\n",
    "- 隐藏层误差项：  \n",
    "  $\\delta_1^h = (\\delta_1^o \\cdot w_5 + \\delta_2^o \\cdot w_7) \\cdot \\frac{\\partial h_1}{\\partial a_1^h} \\approx (0.0083×0.8 + (-0.0010)×1.2)×0.0972 \\approx (0.00664 - 0.0012)×0.0972 \\approx 0.00544×0.0972 \\approx 0.0005$  \n",
    "  $\\delta_2^h = (\\delta_1^o \\cdot w_6 + \\delta_2^o \\cdot w_8) \\cdot \\frac{\\partial h_2}{\\partial a_2^h} \\approx (0.0083×1 + (-0.0010)×0.9)×0.0670 \\approx (0.0083 - 0.0009)×0.0670 \\approx 0.0074×0.0670 \\approx 0.0005$  \n",
    "\n",
    "\n",
    "### 3. 权重梯度与更新  \n",
    "权重梯度公式：$\\frac{\\partial L}{\\partial w_{连接}} = 后层误差项 × 前层输出$  \n",
    "\n",
    "\n",
    "#### （1）输入层→隐藏层权重（$w_1, w_2, w_3, w_4$）  \n",
    "- $w_1$（$x_1→h_1$）：  \n",
    "  梯度 = $\\delta_1^h × x_1 \\approx 0.0005 × 0.5 = 0.0003$  \n",
    "  更新后：$w_1' = w_1 - \\eta×梯度 = 1 - 1×0.0003 = 0.9997$  \n",
    "\n",
    "- $w_2$（$x_2→h_1$）：  \n",
    "  梯度 = $\\delta_1^h × x_2 \\approx 0.0005 × 1.0 = 0.0005$  \n",
    "  更新后：$w_2' = 0.5 - 1×0.0005 = 0.4995$  \n",
    "\n",
    "- $w_3$（$x_1→h_2$）：  \n",
    "  梯度 = $\\delta_2^h × x_1 \\approx 0.0005 × 0.5 = 0.0003$  \n",
    "  更新后：$w_3' = 0.5 - 1×0.0003 = 0.4997$  \n",
    "\n",
    "- $w_4$（$x_2→h_2$）：  \n",
    "  梯度 = $\\delta_2^h × x_2 \\approx 0.0005 × 1.0 = 0.0005$  \n",
    "  更新后：$w_4' = 0.8 - 1×0.0005 = 0.7995$  \n",
    "\n",
    "\n",
    "#### （2）隐藏层→输出层权重（$w_5, w_6, w_7, w_8$）  \n",
    "- $w_5$（$h_1→y_1$）：  \n",
    "  梯度 = $\\delta_1^o × h_1 \\approx 0.0083 × 0.8909 \\approx 0.0074$  \n",
    "  更新后：$w_5' = 0.8 - 1×0.0074 = 0.7926$  \n",
    "\n",
    "- $w_6$（$h_2→y_1$）：  \n",
    "  梯度 = $\\delta_1^o × h_2 \\approx 0.0083 × 0.9278 \\approx 0.0077$  \n",
    "  更新后：$w_6' = 1 - 1×0.0077 = 0.9923$  \n",
    "\n",
    "- $w_7$（$h_1→y_2$）：  \n",
    "  梯度 = $\\delta_2^o × h_1 \\approx -0.0010 × 0.8909 \\approx -0.0009$  \n",
    "  更新后：$w_7' = 1.2 - 1×(-0.0009) = 1.2009$  \n",
    "\n",
    "- $w_8$（$h_2→y_2$）：  \n",
    "  梯度 = $\\delta_2^o × h_2 \\approx -0.0010 × 0.9278 \\approx -0.0009$  \n",
    "  更新后：$w_8' = 0.9 - 1×(-0.0009) = 0.9009$  \n",
    "\n",
    "\n",
    "### 4. 偏置梯度与更新  \n",
    "偏置梯度公式：$\\frac{\\partial L}{\\partial b} = 对应层误差项$  \n",
    "\n",
    "\n",
    "#### （1）隐藏层偏置（$b_1, b_2$）  \n",
    "- $b_1$（隐藏节点1）：  \n",
    "  梯度 = $\\delta_1^h \\approx 0.0005$  \n",
    "  更新后：$b_1' = 1.2 - 1×0.0005 = 1.1995$  \n",
    "\n",
    "- $b_2$（隐藏节点2）：  \n",
    "  梯度 = $\\delta_2^h \\approx 0.0005$  \n",
    "  更新后：$b_2' = 1.5 - 1×0.0005 = 1.4995$  \n",
    "\n",
    "\n",
    "#### （2）输出层偏置（$b_3, b_4$）  \n",
    "- $b_3$（输出节点1）：  \n",
    "  梯度 = $\\delta_1^o \\approx 0.0083$  \n",
    "  更新后：$b_3' = 1 - 1×0.0083 = 0.9917$  \n",
    "\n",
    "- $b_4$（输出节点2）：  \n",
    "  梯度 = $\\delta_2^o \\approx -0.0010$  \n",
    "  更新后：$b_4' = 1.5 - 1×(-0.0010) = 1.5010$  \n",
    "\n",
    "\n",
    "## 五、时间复杂度与节点数关系  \n",
    "1. **时间复杂度**：更新一次网络的计算量与层间连接数成正比，即$O(n_{in}×n_h + n_h×n_{out})$（$n_{in}$=输入节点数，$n_h$=隐藏节点数，$n_{out}$=输出节点数），该网络（输入2→隐藏2→输出2）更新一次的时间复杂度为$O(2×2 + 2×2) = O(8)$（常数级）。  \n",
    "2. **节点数与计算次数关系**：总节点数$N = n_{in} + n_h + n_{out}$，当各层节点同比例增长时，连接数为$O(N^2)$，故**更新一次的计算次数与节点总数的平方成正比**（二次函数关系）。"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
